package flights.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import flights.bi.Admin;
import flights.bi.Airline;
import flights.bi.User;
import flights.db.AdminHandler;
import flights.db.AirlineHandler;
import flights.db.UserHandler;

/**
 * Servlet implementation class AuthenticateServlet
 */
public class AuthenticateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String url = request.getPathInfo();
		response.setContentType( "text/html" ); 
		if (url != null && url.indexOf("member") != -1)
		{
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			User user = null;
			try {
				user = UserHandler.getUserByCredentials(email,password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (user != null)
			{
				out.print("{\"status\":1, \"data\":" +gson.toJson(user) + "}");
				
				Cookie userIdCookie = new Cookie("UserID", "" + user.getUser_id());
				userIdCookie.setMaxAge(60*60*24*14); // 14 days
				userIdCookie.setPath("/");
				
				Cookie userTypeCookie = new Cookie("UserType", "member");
				userTypeCookie.setMaxAge(60*60*24*14); // 14 days
				userTypeCookie.setPath("/");
				
				response.addCookie(userIdCookie);
				response.addCookie(userTypeCookie);
			}
			else
			{
				out.print("{\"status\":0, \"data\":" +gson.toJson(user) + "}");
			}
				
		}
		if (url != null && url.indexOf("air") != -1)
		{
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			Airline airline = null;
			try {
				airline = AirlineHandler.getAirlineByCredentials(email,password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (airline != null)
			{
				out.print("{\"status\":1, \"data\":" +gson.toJson(airline) + "}");
				
				Cookie userIdCookie = new Cookie("UserID", "" + airline.getAirline_id());
				userIdCookie.setMaxAge(60*60*24*14); // 14 days
				userIdCookie.setPath("/");
				
				Cookie userTypeCookie = new Cookie("UserType", "airline");
				userTypeCookie.setMaxAge(60*60*24*14); // 14 days
				userTypeCookie.setPath("/");
				
				response.addCookie(userIdCookie);
				response.addCookie(userTypeCookie);
			}
			else
			{
				out.print("{\"status\":0, \"data\":" +gson.toJson(airline) + "}");
			}
				
		}
		if (url != null && url.indexOf("admin") != -1)
		{
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			Admin admin_user = null;
			try {
				admin_user = AdminHandler.getUserByCredentials(email,password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (admin_user != null)
			{
				out.print("{\"status\":1, \"data\":" +gson.toJson(admin_user) + "}");
								
				Cookie userIdCookie = new Cookie("UserID", "" + admin_user.getAdmin_id());
				userIdCookie.setMaxAge(60*60*24*14); // 14 days
				userIdCookie.setPath("/");
				
				Cookie userTypeCookie = new Cookie("UserType", "admin");
				userTypeCookie.setMaxAge(60*60*24*14); // 14 days
				userTypeCookie.setPath("/");
				
				response.addCookie(userIdCookie);
				response.addCookie(userTypeCookie);
			}
			else
			{
				out.print("{\"status\":0, \"data\":" +gson.toJson(admin_user) + "}");
				
			}
		}
	}

}
